
#include <QtGui>

#include "chatdialog.h"

ChatDialog::ChatDialog( QWidget *parent )
    : QDialog( parent )
{
    vboxLayout = new QVBoxLayout( this );
    vboxLayout->setSpacing( 3 );
    vboxLayout->setContentsMargins( 1, 1, 1, 6 );
    hboxLayout1 = new QHBoxLayout();
    hboxLayout1->setSpacing( 3 );
    hboxLayout1->setContentsMargins( 0, 0, 0, 0 );
    splV = new QSplitter( Qt::Vertical, this );

    messageWindow = new QTextEdit( this );
    contactList = new QListWidget( this );
    messLineEdit = new QLineEdit( this );
    label = new QLabel( this );

    label->setText( "Message:" );

    messageWindow->setFocusPolicy( Qt::NoFocus );
    messageWindow->setReadOnly( true );
    messageWindow->setMinimumHeight( 150 );

    contactList->setFocusPolicy( Qt::NoFocus );
    contactList->setMinimumHeight( 150 );

    splV->addWidget( contactList );
    splV->addWidget( messageWindow );
    vboxLayout->addWidget( splV );
    hboxLayout1->addWidget( label );
    hboxLayout1->addWidget( messLineEdit );
    vboxLayout->addLayout( hboxLayout1 );

    messLineEdit->setFocusPolicy( Qt::StrongFocus );
    messageWindow->setFocusPolicy( Qt::NoFocus );
    messageWindow->setReadOnly(true);
    contactList->setFocusPolicy( Qt::NoFocus );

    connect( messLineEdit, SIGNAL( returnPressed() ), this, SLOT( returnPressed() ) );
    connect( &client, SIGNAL( newMessage( QString, QString ) ), this, SLOT( appendMessage( QString, QString ) ) );
    connect( &client, SIGNAL( newParticipant( QString ) ), this, SLOT( newParticipant( QString ) ) );
    connect( &client, SIGNAL( participantLeft( QString ) ), this, SLOT( participantLeft( QString ) ) );

    myNickName = client.nickName();
    newParticipant( myNickName );
    tableFormat.setBorder( 0 );
    QTimer::singleShot( 10 * 1000, this, SLOT( showInformation() ) );
}

//-------------------------------------

void ChatDialog::appendMessage( const QString & from, const QString & message )
{
    if( from.isEmpty() || message.isEmpty() )
        return;

    QStringList sl = message.split( " " );
    if( sl.at(0) == "WORKERINFO" )
    {
        workerName = sl.at(1);
        workerDims = QRect( sl.at(2).toInt(), sl.at(3).toInt(), sl.at(4).toInt(), sl.at(5).toInt() );
        emit signalFromWorker( workerName, workerDims );
        return;
    }

    QTextCursor cursor( messageWindow->textCursor() );
    cursor.movePosition( QTextCursor::End );
    QTextTable *table = cursor.insertTable( 1, 2, tableFormat );
    table->cellAt( 0, 0 ).firstCursorPosition().insertText( '<' + from + "> " );
    table->cellAt( 0, 1 ).firstCursorPosition().insertText( message );
    QScrollBar *bar = messageWindow->verticalScrollBar();
    bar->setValue( bar->maximum() );
}

//-------------------------------------

void ChatDialog::returnPressed()
{
    QString text = messLineEdit->text();
    if( text.isEmpty() )
        return;

    if( text.startsWith( QChar('/') ) )
    {
        QColor color = messageWindow->textColor();
        messageWindow->setTextColor(Qt::red);
        messageWindow->append(tr( "! Unknown command: %1" ).arg( text.left( text.indexOf(' ') ) ) );
        messageWindow->setTextColor(color);
    }
    else
    {
        client.sendMessage( text );
        appendMessage( myNickName, text );
    }

    messLineEdit->clear();
}

//-------------------------------------

void ChatDialog::newParticipant( const QString & nick )
{
    if( nick.isEmpty() )
        return;

    QColor color = messageWindow->textColor();
    messageWindow->setTextColor( Qt::gray );
    messageWindow->append(tr( "* %1 has joined").arg( nick ) );
    messageWindow->setTextColor( color );
    contactList->addItem( nick );
}

//-------------------------------------

void ChatDialog::participantLeft( const QString & nick )
{
    if( nick.isEmpty() )
        return;

    QList<QListWidgetItem *> items = contactList->findItems( nick, Qt::MatchExactly );
    if( items.isEmpty() )
        return;

    delete items.at( 0 );
    QColor color = messageWindow->textColor();
    messageWindow->setTextColor( Qt::gray );
    messageWindow->append(tr( "* %1 has left" ).arg( nick ) );
    messageWindow->setTextColor( color );
}

//-------------------------------------

void ChatDialog::showInformation()
{
    if( contactList->count() == 1 )
    {
        //QMessageBox::information(this, tr("Chat"), tr("Launch several instances of this program on your local network and start chatting!"));
    }
}

//-------------------------------------

void ChatDialog::sendWorkerInfo( QString n, QRect r )
{
    QString text = QString( "WORKERINFO %1 %2 %3 %4 %5" ).arg( n ).arg( r.x() ).arg( r.y() ).arg( r.width()-50 ).arg( r.height()-50 );
    client.sendMessage( text );
}
